package com.saurshaz.testmvc.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.saurshaz.testmvc.Node;
import com.saurshaz.testmvc.controllers.MyCollectionJsonObject;
import com.saurshaz.testmvc.tree.JsTreeNode;

public class NodeServiceImpl implements NodeService {

	public List<Node> getAll() {
		List<Node> nodeList = new ArrayList<Node>();
		/*
		 * nodeList.add(new Node(1, "node name (1 - 100)", "1", "100", true,
		 * 0)); nodeList.add(new Node(1, "node name (1 - 100)", "1", "100",
		 * true, 0));
		 */
		return nodeList;
	}

	@Override
	public MyCollectionJsonObject getAllNodesAsObject() {

		List<Node> nodes = Node.findAllNodes();

		MyCollectionJsonObject myCollectionJsonObject = new MyCollectionJsonObject();

		Map<String, String> dataMap = new HashMap<String, String>();
		String state = "\"open\"";
		Map<String, String> attributesMap = new HashMap<String, String>();

		List<JsTreeNode> jsTreeNodesList = new ArrayList<JsTreeNode>();

		for (Iterator<Node> itrNodes = nodes.iterator(); itrNodes.hasNext();) {
			Node node = itrNodes.next();
			List<JsTreeNode> childrenNodes = new ArrayList<JsTreeNode>();

			// data properties
			dataMap.put("title", "\"" + node.getName() + "\"");
			dataMap.put("icon", "\"icon\"");

			// attributes
			attributesMap.put("id",
					node.getName() + " -- " + node.getStartRange() + " to "
							+ node.getEndRange());
			attributesMap.put("rel", "\"rel\"");
			attributesMap.put("mdata", "\"mdata\"");

			System.out.println("-------------- node.getIsParent == "
					+ node.getIsParent());
			System.out.println("-------------- node.getIsParent().equals('1') == "
					+ node.getIsParent().equals('1'));			

			// children nodes
			if (node.getIsParent().equals('1')) {
				// state
				state = "\"open\"";

				// add child nodes
				List<Node> childNodes = Node.findByParentNodeId(node
						.getId());
				Map<String, String> dataMapChildren = new HashMap<String, String>();
				String stateChildren = "\"open\"";
				
				Map<String, String> childrenAttributesMap = new HashMap<String, String>();

				// get childeren nodes information
				System.out.println(" ------------------  adding childeren for node with is -  " + node.getId());
				childrenNodes = extractTreeFromNodesList(childNodes,
						dataMapChildren, stateChildren, 
						childrenAttributesMap);
				JsTreeNode jsTreeNode = new JsTreeNode(dataMap, state,
						childrenNodes, attributesMap);
				jsTreeNodesList.add(jsTreeNode);

			} else if (node.getIsParent().equals('0')) {
				// state --- for child level records control should come here
				state = "\"open\"";
				// add to the list
			}

		}

		myCollectionJsonObject.setNodesList(jsTreeNodesList);
		return myCollectionJsonObject;
	}

	private List<JsTreeNode> extractTreeFromNodesList(List<Node> childNodes,
			Map<String, String> dataMapChildren, String stateChildren,
			Map<String, String> childrenAttributesMap) {


		List<JsTreeNode> childrenNodes = new ArrayList<JsTreeNode>();
		System.out.println(" ----------------------- size of childeren list is " + childNodes.size());
		for (Iterator<Node> itrNodes = childNodes.iterator(); itrNodes
				.hasNext();) {
			Node node = itrNodes.next();

			// children nodes
			if (node.getIsParent().equals('0')) {
				// data properties
				dataMapChildren.put("title", "\" child " + node.getName() + "\"");
				dataMapChildren.put("icon", "\"icon\"");
				
				// attributes
				childrenAttributesMap.put("id",
						node.getName() + " --  child  " + node.getStartRange() + " to "
						+ node.getEndRange());
				childrenAttributesMap.put("rel", "\"rel\"");
				childrenAttributesMap.put("mdata", "\"mdata\"");
				
				System.out.println("-------------- node.getIsParent == "
						+ node.getIsParent());

				// state
				stateChildren = "\"open\"";

				// add child nodes
				/*
				 * List<Node> childNodes =
				 * Node.findByParentNodeId(node.getNodeParentId()); Map<String,
				 * String> dataMapChildren = new HashMap<String, String>();
				 * String stateChildren = "\"open\""; List<JsTreeNode>
				 * grandChildrenNodes = new ArrayList<JsTreeNode>(); Map<String,
				 * String> childrenAttributesMap = new HashMap<String,
				 * String>();
				 */
				// get childeren nodes information
				/*
				 * childrenNodes = extractTreeFromNodesList(childNodes,
				 * dataMapChildren, stateChildren, grandChildrenNodes,
				 * childrenAttributesMap);
				 */

				JsTreeNode jsTreeNode = new JsTreeNode(dataMapChildren,
						stateChildren, new ArrayList<JsTreeNode>(),
						childrenAttributesMap);
				childrenNodes.add(jsTreeNode);

			} else if (node.getIsParent().equals('1')) {
				// state --- for child level records control should come here
				// stateChildren = "\"closed\"";
				// add to the list
			}

		}
		return childrenNodes;

	}
}
